package com.gome.todo;

import com.gome.vue4j.core.AbstractVue4jComponent;
import com.gome.vue4j.core.FXMLPath;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@FXMLPath("/todo/todo-manager.fxml")
public class TodoManager extends AbstractVue4jComponent {

    @FXML
    StringProperty inTodo = new SimpleStringProperty();

    @FXML
    List<Todo> list = new ArrayList<>(Arrays.asList(
            new Todo("盘古MES二开"),
            new Todo("用友NC会议"),
            new Todo("泛微OA技术支持")
    ));

    @FXML
    void removeTodo(int index){
        list.remove(index);
        update();
    }

    @FXML
    void insertTodo(KeyEvent event){
        if (event.getCode() == KeyCode.ENTER) {
            addTodo(event);
        }
    }

    @FXML
    void addTodo(Event event){
        String text = inTodo.get();
        if(text != null && text.length() != 0){
            Todo todo = new Todo();
            list.add(todo);

            todo.setText(text);
            todo.setCreateTime(new Date());
            inTodo.set("");
            update();
        }
    }
}
